package cn.itcast.jx.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import cn.itcast.jx.dao.BaseDao;
import cn.itcast.jx.domain.Contract;
import cn.itcast.jx.domain.ContractProduct;
import cn.itcast.jx.domain.Export;
import cn.itcast.jx.domain.ExportProduct;
import cn.itcast.jx.domain.ExtCproduct;
import cn.itcast.jx.domain.ExtEproduct;
import cn.itcast.jx.service.ExportService;
import cn.itcast.jx.util.Page;

public class ExportServiceImpl implements ExportService{
	private BaseDao baseDao;
	
	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	@Override
	public List<Export> find(String hql, Class<Export> entityClass, Object[] params) {
		return baseDao.find(hql, entityClass, params);
	}

	@Override
	public Export get(Class<Export> entityClass, Serializable id) {
		return baseDao.get(entityClass, id);
	}

	@Override
	public Page<Export> findPage(String hql, Page<Export> page,
			Class<Export> entityClass, Object[] params) {
		return baseDao.findPage(hql, page, entityClass, params);
	}

	@Override
	public void saveOrUpdate(Export entity) {
		if(StringUtils.isBlank(entity.getId())){
			//新增
			String[] ids = entity.getContractIds().split(", ");
			for (String id : ids) {
				Contract contract = baseDao.get(Contract.class, id);
				contract.setState(1);
				baseDao.saveOrUpdate(contract);
			}
			
			entity.setInputDate(new Date());
			entity.setState(0);
			//"a,b,c"
			String idStr = "";
			for (String id : ids) {
				idStr += "'" + id + "',";
			}
			idStr = idStr.substring(0,idStr.length()-1);
			
			String hql = "from ContractProduct where contract.id in(" + idStr + ")";
			List<ContractProduct> cpList = baseDao.find(hql, ContractProduct.class, null);
			HashSet<ExportProduct> epSet = new HashSet<ExportProduct>();
			
			//遍历购销合同下所有的货物，并存到报运商品里
			for (ContractProduct cp : cpList) {
				ExportProduct ep = new ExportProduct();
				ep.setFactory(cp.getFactory());
				ep.setProductNo(cp.getProductNo());
				ep.setPackingUnit(cp.getPackingUnit());
				ep.setCnumber(cp.getCnumber());
				ep.setPrice(cp.getPrice());
				
				epSet.add(ep);
				ep.setExport(entity);
				//遍历每个货物下的附件
				Set<ExtCproduct> extList = cp.getExtCproducts();
				HashSet<ExtEproduct> exteList = new HashSet<ExtEproduct>();
				
				for (ExtCproduct extc : extList) {
					ExtEproduct exte = new ExtEproduct();
					BeanUtils.copyProperties(extc, exte);
					exte.setId(null);
					exte.setExportProduct(ep);
					exteList.add(exte);
				}
				ep.setExtEproducts(exteList);
			}
			
			entity.setExportProducts(epSet);
			
			baseDao.saveOrUpdate(entity);
			
		}else{
			//修改
			Export export = baseDao.get(Export.class, entity.getId());
			
			baseDao.saveOrUpdate(export);
		}
		
	}

	@Override
	public void saveOrUpdateAll(Collection<Export> entitys) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteById(Class<Export> entityClass, Serializable id) {
		baseDao.deleteById(entityClass, id);
	}

	@Override
	public void delete(Class<Export> entityClass, Serializable[] ids) {
		for(Serializable id:ids){
			deleteById(entityClass, id);
		}
	}
	
}
